package com.zhuss.product.processor;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.product.line.EnableLineBo;
import com.zhuss.bean.vo.product.line.DetailLineVo;
import com.zhuss.common.AbstractProcessor;
import com.zhuss.common.Processor;
import com.zhuss.db.entity.OpLog;
import com.zhuss.enums.LogActionEnum;
import com.zhuss.enums.MoudleEnum;
import com.zhuss.exception.DzcxException;
import com.zhuss.exception.ExceptionCode;
import com.zhuss.product.db.dao.ext.LineDao;
import com.zhuss.product.db.dao.ext.LineStrategyDao;
import com.zhuss.product.db.entity.Line;
import com.zhuss.product.processor.cache.LineCache;
import com.zhuss.util.JsonUtil;

/**
 * 激活、置为模板
 * 线路 processor
 * @Author: zhushanshuo
 * @Date: Wed Jan 06 09:47:31 CST 2021
 */
@Component("lineEnable")
public class LineEnable extends AbstractProcessor<Object, EnableLineBo> implements Processor<Object, EnableLineBo> {

    @Autowired
    private LineDao lineDao;

    @Autowired
	@Qualifier("lineRead")
	private LineRead lineRead;

    @Autowired
    private LineCheck lineCheck;

	@Autowired
	@Qualifier("lineCache")
	private LineCache lineCache;

	@Autowired
	private LineStrategyDao lineStrategyDao;

    /**
     * 校验
     */
    @Override
    public void check(EnableLineBo bo) throws DzcxException {

    	if (bo.getLineId() == null || bo.getLineId() <=0l) {
			throw new DzcxException(ExceptionCode.线路ID为空);
		}
    	// 当前线路不满足置为模板条件
		if (!lineCheck.checkEnable(bo.getLineId())) {
			throw new DzcxException(ExceptionCode.当前数据不满足置为模板);
		}
    }

    /**
     * 操作方法
     * @throws DzcxException
     */
    @Override
    public Object process(EnableLineBo param) throws DzcxException {

    	Long lineId = param.getLineId();

    	Line record = new Line();
    	record.setLineId(param.getLineId());
    	record.setPretype(param.getPretype());

    	record.setModifierId(param.getConcurrentUserId());
    	record.setModifier(param.getConcurrentUserName());
    	record.setModifyTime(new Date());
    	lineDao.updateByPrimaryKeySelective(record);

    	String name = ((DetailLineVo) lineRead.detail(lineId)).getName();

    	param.setName(name);

    	lineCache.clear(lineId);
        return null;
    }

    /**
     * 操作日志
     */
	@Override
	public Object log(EnableLineBo param) throws DzcxException {
		OpLog record = new OpLog();
    	record.setMoudle(MoudleEnum.商品.key());
    	record.setAction(LogActionEnum.置为模板.key());
    	record.setTargetId(param.getLineId());
    	record.setTargetName(param.getName());
    	record.setContent("参数: " + JsonUtil.toJson(param));
    	record.setReason(param.getExceptionReason());

    	record.setCreatorId(param.getConcurrentUserId());
    	record.setCreator(param.getConcurrentUserName());
    	record.setCreateTime(new Date());
    	log(record);
    	return null;
	}
}
